package nero.yncc.com.createart.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.bean.ChangjingBean;

/**
 * Created by nero on 2017/6/26.
 */

public class AdapterDetailChangjing extends BaseAdapter {

    Context context;
    ArrayList<ChangjingBean.ChangjingInBean> arrayList;
    ChildClick childClick;

    public int viewHeight = 0;

    public AdapterDetailChangjing(Context context, ArrayList<ChangjingBean.ChangjingInBean> arrayList, ChildClick childClick) {
        this.context = context;
        this.arrayList = arrayList;
        this.childClick = childClick;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        Holder holder;

        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.adapter_detail_changjing, null, false);
            holder = new Holder(view);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        if (viewHeight == 0) {
            view.measure(0, 0);
            viewHeight = view.getMeasuredHeight();
        }

        ChangjingBean.ChangjingInBean bean = arrayList.get(i);
        holder.tv_content.setText(bean.getName());
        if (bean.isChoose()) {
            holder.tv_content.setBackgroundResource(R.drawable.fill_yellow);
            holder.tv_content.setTextColor(Color.WHITE);
        } else {
            holder.tv_content.setBackgroundResource(R.drawable.border_gray_deep);
            holder.tv_content.setTextColor(context.getResources().getColor(R.color.textColorGray_deep));
        }

        holder.ll_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                childClick.click(i);
            }
        });
        return view;
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView tv_content;
        LinearLayout ll_root;

        public Holder(View itemView) {
            super(itemView);
            tv_content = itemView.findViewById(R.id.tv_content);
            ll_root = itemView.findViewById(R.id.ll_root);
        }
    }

    public interface ChildClick {
        void click(int position);
    }

}
